package com.haoting.blogservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haoting.blogservice.entity.Blog;
import com.haoting.blogservice.entity.BlogContent;
import com.haoting.blogservice.entity.vo.BlogInfoVo;
import com.haoting.blogservice.entity.vo.BlogPublishVo;
import com.haoting.blogservice.entity.vo.BlogQuery;
import com.haoting.blogservice.entity.vo.BlogWebVo;
import com.haoting.blogservice.entity.vo.ConsumerQuery;
import com.haoting.blogservice.entity.vo.FrontAuthorVo;
import com.haoting.blogservice.entity.vo.HotAuhtor;
import com.haoting.blogservice.service.BlogService;
import com.haoting.commonutils.CommonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tingting
 * @since 2020-12-21
 */
@Api(tags = "文章管理")
@RestController
@RequestMapping("/blogservice/blog")
//@CrossOrigin
public class BlogController {

    @Autowired
    private BlogService blogService;

    //添加文章基本信息
    @PostMapping("addBlogInfo")
    public CommonResult addBlogInfo(@RequestBody BlogInfoVo blogInfoVo){
        String id = blogService.saveBlogInfo(blogInfoVo);

        return CommonResult.ok().data("blogId",id);
    }
    @GetMapping("getBlogById/{blogId}")
    public CommonResult getBlogById(@PathVariable String blogId){
        Blog blog = blogService.getById(blogId);
        return CommonResult.ok().data("blog",blog);
    }

    @PostMapping("updateBlog")
    public CommonResult updateBlog(@RequestBody Blog blog){
        Boolean update = blogService.updateById(blog);
        if(update){
            return CommonResult.ok();
        }else {
            return CommonResult.error();
        }
    }

    //根据文章Id查询文章最终信息
    @GetMapping("getPublishBlogInfo/{id}")
    public CommonResult getPublishBlogInfo(@PathVariable String id){
        BlogPublishVo blogPublishVo = blogService.publishBlogInfo(id);
        return CommonResult.ok().data("publishBlog",blogPublishVo);
    }

    //修改文章状态
    @PostMapping("publishBlog/{id}")
    public CommonResult publishBlog(@PathVariable String id){
        Blog blog = new Blog();
        blog.setId(id);
        blog.setStatus("Normal");
        blogService.updateById(blog);
        return CommonResult.ok();
    }
    //分页条件查询文章列表
    @PostMapping("getBlogList/{current}/{limit}")
    public CommonResult getBlogList(@PathVariable long current, @PathVariable long limit,
                                    @RequestBody(required = false) BlogQuery blogQuery){
        Page<BlogInfoVo> blogPage = new Page<>(current,limit);
        blogService.getPageVo(blogPage,blogQuery);
        long total = blogPage.getTotal();
        List<BlogInfoVo> records = blogPage.getRecords();
        return CommonResult.ok().data("total",total).data("rows",records);
    }

        @GetMapping("getHotAuthor")
        public CommonResult getHotAuthor(){
            List<HotAuhtor> hotAuthor = blogService.getHotAuthor();

            return CommonResult.ok().data("hotAuthor",hotAuthor);

        }

        @GetMapping("getFrontBlogInfo/{blogId}")
        public CommonResult getFrontBlogInfo(@PathVariable String blogId){
                BlogWebVo blogWebVo = blogService.getFrontBlogInfo(blogId);

                return CommonResult.ok().data("blogWebVo",blogWebVo);
        }

        @GetMapping("getAuthorCount/{authorId}")
        public CommonResult getAuthorCount(@PathVariable String authorId){
            FrontAuthorVo frontAuthorVo = blogService.getAuthorCount(authorId);
            return CommonResult.ok().data("authorInfo",frontAuthorVo);
        }

        @PostMapping("addSupportCount/{blogId}")
        public CommonResult addSupportCount(@PathVariable String blogId){
               blogService.addSupportCount(blogId);
               return CommonResult.ok();
        }

        @PostMapping("collectionUpdate/{blogId}")
        public CommonResult collectionUpdate(@PathVariable String blogId){
                blogService.collectionUpdate(blogId);
                return CommonResult.ok();
        }





}

